<?php 
namespace app\admin\controller;

class Goodslabel extends Base{
	public function index(){
		$keywords = input('keywords');
		$label = db('goods_label')->alias('l')
		->field('l.*,c.cate_name as cname')
		->join('sdzx_goods_category c','c.cate_id = l.cate_id');

		if($keywords){
			$where = ['label_name' => $keywords];
			$label = $label->where($where);
		}
		
		$label = $label->order('label_sort asc')->select();
		
		$this->assign('list',$label);
		return $this->fetch();
	}
	
	public function edit_sort(){
		if (request()->isPost()) {
			$list=json_decode(input('list'), true);

			foreach($list as $val){
				db('goods_label')->where('label_id',$val['id'])->setField('label_sort',$val['listorder']);
			}
			$this->success('操作成功');
		}
	}
	
	public function add(){
		if(request()->isPost()){
			$data = request()->only('label_name,cate_id,label_sort','post');
			$re = db('goods_label')->insertGetId($data);
			if ($re) {
				$this->success('新增成功');
			}else{
				$this->error('新增失败');
			}
		}else{
			$cat = CAT('goods_category',array('cate_id','cate_pid','cate_name','fullname'),'cate_status=1');
			$cate = $cat->getList(0);
			// $cate = db('goods_category')->select();
			$this->assign('cate',$cate);
			return $this->fetch();
		}
	}
	
	public function edit(){
		if(request()->isPost()){
			$data = request()->only('label_id,label_name,label_sort,cate_id','post');
			if (!isset($data['label_id']) || empty($data['label_id'])) {
				$this->error('参数错误');
			}

			$re = db('goods_label')->where(['label_id'=>$data['label_id']])->update($data);
			if ($re!==false) {
				$this->success('操作成功');
			}else{
				$this->error('操作失败');
			}
		}else{
			$info = db('goods_label')->where('label_id',input('label_id'))->find();
			$cat = CAT('goods_category',array('cate_id','cate_pid','cate_name','fullname'),'cate_status=1');
			$cate = $cat->getList(0);
			// $cate = db('goods_category')->select();
			
			$this->assign('cate',$cate);	
			$this->assign('info',$info);
			return $this->fetch();
		}
	}
	
	public function delete(){
		$label_id = input('ids');
		
		if($label_id){
			$res = db('goods_label')->delete($label_id);
			if($res){
				$this->success('删除成功');
			}
		}
	}
}
?>